package br.siseventos.view.conversores;

import br.siseventos.model.TbEvento;
import br.siseventos.model.TbLocal;
import br.siseventos.service.LocalService;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "localConversor")
public class ConversorLocal extends BaseConverter {
    // <editor-fold desc="Campos">
    private LocalService localService = new LocalService();
    // </editor-fold>
    // <editor-fold desc="Construtor">
    public ConversorLocal() {
    }
    // </editor-fold>
    // <editor-fold desc="Sobrescrita">
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        // Transformar string em objeto
        TbLocal local = null;
        try {
            local = localService.consultar(Integer.parseInt(value));
        } catch (Exception ex) {
        }
        return local;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        // Transformar objeto em string
        String r = null;
        try {
            if (value instanceof TbLocal) {
                r = ((TbLocal) value).getIdtLocal().toString();
            }
        } catch (Exception e) {
        }

        return r;
    }
// </editor-fold>
}
